<?php

    namespace app\store\model;

    use think\Db;
    use think\Model;

    class GoodsModel extends Model
    {
        const PRICE_VIEW_NOTHING = 0;
        const PRICE_VIEW_POINT = 1;
        const PRICE_VIEW_PRICE = 2;
        const PRICE_VIEW_PRICE_POINT = 3;


        protected $name = 'store_goods';

        protected $autoWriteTimestamp = true;

        protected $type = ['create_time' => 'timestamp:Y-m-d H:i', 'update_time' => 'timestamp:Y-m-d H:i'];

        public $page_info;


        /**
         * 商品保存
         *
         * @param    array $data 商品资料
         *
         * @return bool|int
         */
        public function addStore($data)
        {

            return self::create($data);

        }

        static public function disMember()
        {

            return Db::name('store_user')->where(['status' => 1])->column('member_id');
        }

        /**
         * 商品列表
         *
         * @param        $where
         * @param null $page
         * @param string $order
         * @param string $field
         *
         * @return \think\Paginator
         * @throws \think\exception\DbException
         */
        static public function lists($where = [], $listRows = 10, $order = 'order asc', $field = '*')
        {

            $goods_lists = self::alias('store')->where($where)->where('status = 1')->field($field)->order($order)->paginate($listRows, false, ['query' => request()->param()]);

            $goods_lists = $goods_lists->toArray();
            $module_config = module_config('store');
            $goods_lists['config'] = $module_config;
            foreach ($goods_lists['data'] as &$datum) {

                unset($datum['price'], $datum['point']);//去除商品列表价格和积分

//                if ($module_config['store_price_view'] == self::PRICE_VIEW_NOTHING) {
//                    unset($datum['price'], $datum['point']);
//                }
//                if ($module_config['store_price_view'] == self::PRICE_VIEW_POINT) {
//                    unset($datum['price']);
//                }
//                if ($module_config['store_price_view'] == self::PRICE_VIEW_PRICE) {
//                    unset($datum['point']);
//                }
                $datum['image'] = get_file_path($datum['image']);
                $datum['images'] = get_files_path($datum['images']);
                $datum['status'] = 0;
            }
            return $goods_lists;

        }

        /**
         * 商品详情
         *
         * @param        $goods_id
         * @param string $field
         *
         * @return array|false|\PDOStatement|string|Model
         */
        static public function getDetail($goods_id, $field = '*')
        {

            $goods_lists = self::field($field)->where(['id' => $goods_id, 'status' => 1])->find();
            if (!$goods_lists) {
                return [];
            }

            $datum['status'] = 0;
            return $goods_lists;
        }

        /**
         * 商品信息更新
         *
         * @param    array $data 列表条件
         * @param    int $id 商品id
         *
         * @return bool|int
         */
        public function editStore($data, $id)
        {

            if (empty($data)) {
                return false;
            }
            if (is_array($id)) {
                $id = implode(',', $id);
                $update = self::where('id', 'in', $id)->update($data);
            } else {
                $update = self::where('id', $id)->update($data);
            }

            //得到条件语句

            return $update;
        }

        /**
         * 电商物品数量
         *
         * @param array $where 电商物品资料
         *
         * @return mixed
         */
        public function countGoods($where)
        {

            $goods_array = self::where($where)->count();
            return $goods_array[0][0];
        }

        /**
         * 电商物品删除
         *
         * @param    int $id 商品id
         *
         * @return bool|int
         */
        public function delStore($id)
        {

            if (empty($id)) {
                return false;
            }
            if (is_array($id)) {
                $del_state = self::where('id', 'in', $id)->delete();
            } else {
                $del_state = self::where('id', $id)->delete();
            }

            return $del_state;
        }

    }